<?php 
global $_GPC, $_W;
$shopid=check_right(2);
if(!$shopid)message("请先选择需要管理的店铺",$this->createWebUrl('shop'),'error');
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];

if($operation=="display"){
	$goodpcate = pdo_fetchall("SELECT id,title FROM ".tablename('j_hmoney_goodcategory')." WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by id desc");
	$goodary = array();
	foreach ($goodpcate as $row) {
		$goodary[$row['id']]=$row['title'];
	}
} elseif ($operation == 'getinfo') {
	$condition = " and groupid ='".$shopid."' ";
	switch(intval($_GPC['datetype'])){
		case "1":
			$condition .= " and createdate='".date('Y-m-d')."' ";
		break;
		case "2":
			$condition .= " and createdate='".date('Y-m-d',strtotime("-1 day"))."' ";
		break;
		case "3":
			$condition .= " and createdate>='".date('Y-m-d',strtotime("-6 day"))."' ";
		break;
		case "4":
			$condition .= " and createtime>='".strtotime($_GPC['statrtime'])."' and createtime<='".strtotime($_GPC['endtime'])."' ";
		break;
	}
	$list_id = pdo_fetchall("SELECT id FROM ".tablename('j_hmoney_order')." WHERE weid='{$_W['uniacid']}' $condition and ispay=1");
	$idary=array();
	foreach($list_id as $row){
		$idary[]=$row['id'];
	}
	if(!$list_id)die(json_encode(array("success"=>false)));
	$goodinfo = pdo_fetch("SELECT sum(total_fee) as num1,sum(num) as num2 FROM ".tablename('j_hmoney_ordergoods')." WHERE orderid in (".implode(',',$idary).") and ispay=1");
	$goodinfo2 = pdo_fetchall("SELECT sum(total_fee) as num1,sum(num) as num2,title,goodid FROM ".tablename('j_hmoney_ordergoods')." WHERE orderid in (".implode(',',$idary).") and ispay=0 group by goodid order by num1 desc ,num2 desc,id desc limit 10");
	
	$sql="SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.pcate FROM ".tablename('j_hmoney_ordergoods')." a left join ".tablename('j_hmoney_goods')." b on a.goodid=b.id WHERE a.orderid in (".implode(',',$idary).") and a.ispay=1 group by b.pcate";
	$goodinfo3 = pdo_fetchall($sql);
	
	die(json_encode(array("success"=>true,"num"=>count($idary),"goodinfo"=>$goodinfo,"goodinfo2"=>$goodinfo2,"goodinfo3"=>$goodinfo3)));

} elseif ($operation == 'outputs') {
    $condition = " and groupid ='".$shopid."' ";
    switch(intval($_GPC['datetype'])){
        case "1":
            $condition .= " and createdate='".date('Y-m-d')."' ";
            break;
        case "2":
            $condition .= " and createdate='".date('Y-m-d',strtotime("-1 day"))."' ";
            break;
        case "3":
            $condition .= " and createdate>='".date('Y-m-d',strtotime("-6 day"))."' ";
            break;
        case "4":
            $condition .= " and createtime>='".strtotime($_GPC['statrtime'])."' and createtime<='".strtotime($_GPC['endtime'])."' ";
            break;
    }
    $list_id = pdo_fetchall("SELECT id FROM ".tablename('j_hmoney_order')." WHERE weid='{$_W['uniacid']}' $condition and ispay=1");
    $idary=array();
    foreach($list_id as $row){
        $idary[]=$row['id'];
    }
    if(!$list_id)die(json_encode(array("success"=>false)));
    $goodlist = pdo_fetchall("SELECT sum(total_fee) as fee1,sum(num) as num1,max(title) as til,max(title) as til,max(goodsn) as sn,max(oldprice) as price1 FROM ".tablename('j_hmoney_ordergoods')." WHERE orderid in (".implode(',',$idary).") and ispay=1 group by goodid order by sum(total_fee) desc");

    require_once "../framework/library/phpexcel/PHPExcel.php";
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setCreator("智慧收银台")
        ->setLastModifiedBy("智慧收银台")
        ->setTitle("数据EXCEL导出")
        ->setSubject("数据EXCEL导出")
        ->setDescription("备份数据")
        ->setKeywords("excel")
        ->setCategory("result file");
    //单号	消费对象	数量	订单金额	茶位	实付金额	优惠	支付方式	收银员	时间	状态	备注
    $excelAry=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","X","T","U","V","W","X","Y","Z");
    $headerAry=array("条码","名称","原单价","销售金额","数量","实销单价","折扣率");
    for($i=0;$i<count($headerAry);$i++){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelAry[$i].'1',$headerAry[$i]);
    };
    $num=2;
    foreach($goodlist as $row){
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$num, "'".$row['sn'])
            ->setCellValue('B'.$num, $row['til'])
            ->setCellValue('C'.$num, $row['price1'])
            ->setCellValue('D'.$num, $this->_2money(($row['fee1'])))
            ->setCellValue('E'.$num, $row['num1'])
            ->setCellValue('F'.$num, $this->_2money(($row['fee1']/$row['num1'])))
            ->setCellValue('G'.$num, $this->_2money((($row['fee1']/$row['num1']/($row['price1']*100))*100)*100)."%");
        $num++;
    }
    $objPHPExcel->getActiveSheet()->setTitle('商品销售记录');
    $objPHPExcel->setActiveSheetIndex(0);
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename="salegoods_temp.xls"');
    header('Cache-Control:max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit();
	
}
include $this->template('web/'.$cfg['floder'].'/salegoods');